<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RSA加密管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" th:href="@{/plugins/layui/css/layui.css}"/>
</head>
<body>
<!-- 模块名 -->
<div class="layui-container" style="width: 100%;">
    <div class="layui-row">
        <div class="layui-col-md12">
            <!-- 模块名 -->
            <blockquote class="layui-elem-quote" style="border-left: 5px solid #3385FF;" th:text="RSA加密解密测试"></blockquote>
            <div th:text="${data.modu}"></div>
        </div>
    </div>
</div>
<!-- 表格标签工具栏 -->

<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js"></script>
<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn2.js"></script>
<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/prng4.js"></script>
<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rng.js"></script>
<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js"></script>
<script language="JavaScript" type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rsa2.js"></script>

<div style="margin-left: 15px;"><br><br>
    <button onclick="getModule()">1、获取加密秘钥,只有拿到秘钥才可以对文本加密</button><br><br>
    <span id="key"></span><br>
</div>
<div style="margin-left: 15px;"><br><br>
    <button onclick="testForFrontEnd()">2、前端测试对[12345]加密解密</button><br><br>
    <span id="explain"></span><br>
    <span id="encrypt"></span><br>
    <span id="privateKey"></span><br>
    <span id="decrypt"></span><br>
</div>
<div style="margin-left: 15px;"><br><br>
    <button onclick="testForBackEnd()">3、后端测试对[12345]解密</button><br><br>
    <span id="explainForBackEnd"></span><br>
    <span id="encryptForBackEnd"></span><br>
    <span id="privateKeyForBackEnd"></span><br>
    <span id="decryptForBackEnd"></span><br>
</div>

<script>
    //2声明加密对象
    var RSADo = {
        "modulus":"",
        "publicExponent":""}

    var privateKey = "";

    /**
     * 前端测试加密解密
     */
    function testForFrontEnd(){
        const str = "12345"
        const rsa = new RSAKey()
        rsa.setPublic(RSADo.modulus, RSADo.publicExponent)
        const encryptStr = rsa.encrypt(str);
        console.log('modulus：', RSADo.modulus);
        console.log('明文（explain）：', str)
        console.log('加密后（encrypt）：', encryptStr)
        $("#explain").text('明文：' + str)
        $("#encrypt").text('加密后：' + encryptStr)

        // 解密
        rsa.setPrivate(RSADo.modulus, RSADo.publicExponent, privateKey)
        const decryptStr = rsa.decrypt(encryptStr)
        console.log('解密私钥：', privateKey)
        console.log('解密后（decrypt）', decryptStr)
        $("#privateKey").text('解密私钥：' + privateKey)
        $("#decrypt").text('解密后（decrypt）：' + decryptStr)
    }

    /**
     * 后端测试
     */
    function testForBackEnd(){
        const str = "12345"
        const rsa = new RSAKey()
        rsa.setPublic(RSADo.modulus, RSADo.publicExponent)
        const encryptStr = rsa.encrypt(str);
        console.log('加密后（encrypt）：', encryptStr)
        $("#explainForBackEnd").text('明文：' + str)
        $("#encryptForBackEnd").text('加密后：' + encryptStr)
        $("#privateKeyForBackEnd").text('解密私钥：' + privateKey)
        $.ajax({url:"/rsa/decryption",
            data:{
                encryptStr: encryptStr
            },
            success:function(result){
                console.log("xxxx："+JSON.stringify(result));
                $("#decryptForBackEnd").text('解密后（decrypt）：' + result.data)
            }
        });
    }

    /**
     * 获取加密信息
     */
    function getModule(){
        $.ajax({url:"/rsa/key",
            data:{},
            success:function(result){
                RSADo.modulus = result.data.modu;
                RSADo.publicExponent = result.data.g;
                privateKey = result.data.m;
                $("#key").text("用于加密的key：" + privateKey);
            }
        });
    }

</script>
</body>
<!--引入js文件-->
<script type="text/javascript" th:src="@{/javascript/jquery-2.1.4.js}"></script>
<script type="text/javascript" th:src="@{/plugins/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/user/user.js}"></script>
</html>